#include <system.h>
#include "kegscale_mk2.h"
#include "eeprom_utils.h"
#include "utils.h"
#include "display.h"

void set_keg_full_menu() {
    uint8_t page = 0;
    uint8_t lcd_height = eeprom_read(LCD_HEIGHT_ADDR);
    uint8_t num_kegs   = eeprom_read(NUM_KEGS_ADDR);
    uint8_t num_pages = get_num_pages();
    uint8_t row = 0;
    while (page < num_pages) {
        update_display(page);
        set_pints_left(row, '*', '*');
        // wait for a button press or long-press
        while (! button_trigger && ! long_button_trigger)
            clear_wdt();
        if (long_button_trigger) {
            long_button_trigger = 0;
            uint8_t keg_num = page * lcd_height + row;
            // write 'full_keg' value
            uint32_t keg_full = get_keg_level(keg_num);
            uint8_t offset = CALC_KEGINFO_OFFSET(keg_num);
            eeprom_uint24_write(offset + KEGINFO_KEG_FULL_OFFSET, keg_full);
            cur_keg = 0;
            return;
        }
        button_trigger = 0;
        // a short button press
        row++;
        if (page * lcd_height + row > num_kegs)
            return;
        if (row == lcd_height) {
            row = 0;
            page++;
        }
    }
}
